package org.itsky.study.test3;

import org.itsky.study.test2.Employee;
import org.itsky.study.test2.EmployeeData;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * @Author sky
 * @Site cmtianxie163.com 2020/4/13 13:53
 */
public class StreamAPITest3 {
    public static void main(String[] args) {
        List<Employee> employees = EmployeeData.getEmployees();
        /**
         * 1. 匹配与查找
         */

        //是否所有员工年龄都大于18
        boolean allMatch = employees.stream().allMatch(e -> e.getAge() > 18);
        System.out.println(allMatch);
        //是否存在员工姓 孙
        boolean noneMatch = employees.stream().noneMatch(e -> e.getName().startsWith("孙"));
        System.out.println(noneMatch);
        //返回第一个元素
        Optional<Employee> first = employees.stream().findFirst();
        Employee employee = first.get();
        System.out.println(employee);
        //返回当前流中的任意元素
        Employee employee1 = employees.parallelStream().findAny().get();
        System.out.println(employee1);
        //返回流中元素总个数
        long count = employees.stream().count();
        System.out.println(count);
        //返回最高工资
        Stream<Double> doubleStream = employees.stream().map(e -> e.getSalary());
        Double maxSalary = doubleStream.max(Double::compare).get();
        System.out.println(maxSalary);
        //返回最低工资的员工
        Employee minSalaryEmp = employees.stream().min((o1, o2) -> Double.compare(o1.getSalary(), o2.getSalary())).get();
        System.out.println(minSalaryEmp);
        //内部迭代
        employees.stream().forEach(System.out::println);
        //集合遍历
        employees.forEach(System.out::println);

        /**
         * 2. 规约
         */
        //计算 1-10 的自然数之和
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        Integer sum = list.stream().reduce(0, Integer::sum);
        System.out.println(sum);
        //计算公司所有员工工资的总和
        Optional<Double> sumSalary = employees.stream().map(e -> e.getSalary()).reduce((s1, s2) -> s1 + s2);
        System.out.println(sumSalary.get());
    }
}
